//
//  RJCityTable.m
//  AirQuality
//
//  Created by Po on 15/3/12.
//  Copyright (c) 2015年 Po. All rights reserved.
//

#import "RJCityTable.h"
@interface RJCityTable () <UITableViewDelegate, UITableViewDataSource>

@property (retain, nonatomic)UITableView * tableView;
@property (retain, nonatomic)NSArray * cityArray;                       //城市数据
@property (retain, nonatomic)NSMutableArray * currentDataArray;         //当前数据

@property (retain, nonatomic)UIImageView * pointImage;                  //地点表示图标
@property (retain, nonatomic)UIImageView * arrowImage;                  //选择箭头
@property (retain, nonatomic)UILabel * cityTitleLabel;                  //选中城市

@property (assign, nonatomic)BOOL isShow;                               //是否正在展示
@property (assign, nonatomic)BOOL isMoving;                             //是否正在动画
@property (assign, nonatomic)CGRect trueRect;

- (void)initData;
- (void)initInterface;
@end


@implementation RJCityTable

- (instancetype)initWithFrame:(CGRect)frame
{
    _trueRect = frame;
    self = [super initWithFrame:frame];
    if (self) {
        [self initData];
        [self initInterface];
    }
    return self;
}

#pragma mark - user-defined initialization function
- (void)initData
{
    _isShow = NO;
    _currentRow = 0;
}

- (void)initInterface
{
    //self init
    CGRect rect = _trueRect;
    rect.size.height = 30;
    [self setFrame:rect];
    [self addSubview:[self getCityBar]];
//    [self addSubview:[self getTableView]];
}

/**获取表格*/
- (UITableView *)getTableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame))];
        [_tableView setDataSource:self];
        [_tableView setDelegate:self];
        [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
        [_tableView setBackgroundColor:[UIColor clearColor]];
    }
    return _tableView;
}

/**表头*/
- (UIView *)getCityBar
{
    if (!_cityBar) {
        _cityBar = [[UIView alloc] init];
        [_cityBar setBackgroundColor:[UIColor clearColor]];
        //左侧图标
        
        [_cityBar addSubview:[self getCityTitleLabel]];
        [_cityBar addSubview:[self getPointImage]];
        
        [_cityBar addSubview:[self getArrowImageWithPoint:CGPointMake(CGRectGetWidth(self.frame) - 20, 5)]];
        UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didPressCityButton)];
        [tapGesture setNumberOfTapsRequired:1];
        [_cityBar addGestureRecognizer:tapGesture];
    }
    return _cityBar;
}
/**按钮箭头*/
- (UIImageView *)getArrowImageWithPoint:(CGPoint)point
{
    if (!_arrowImage) {
        _arrowImage = [[UIImageView alloc] initWithFrame:CGRectMake(point.x, point.y, 12, 20)];
        [_arrowImage setImage:[UIImage imageNamed:@"cityArrow"]];
    }
    return _arrowImage;
}

- (UIImageView *)getPointImage
{
    if (!_pointImage) {
        _pointImage = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 16, 20)];
        [_pointImage setImage:[UIImage imageNamed:@"cityChoose"]];
    }
    return _pointImage;
}

/**城市*/
- (UILabel *)getCityTitleLabel
{
    if (!_cityTitleLabel) {
        _cityTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, CGRectGetWidth(self.frame) - 20, 20)];
        [_cityTitleLabel setText:[RJUserData defaultRJUserData].currentCity];
        [_cityTitleLabel setTextAlignment:NSTextAlignmentCenter];
        [_cityTitleLabel setTextColor:[UIColor whiteColor]];
    }
    return _cityTitleLabel;
}

#pragma mark - event
- (void)didPressCityButton
{
    _isShow = !_isShow;
    [self setViewFrame:_isShow];
}

#pragma mark - function
- (NSArray *)getCitys
{
    return _cityArray;
}

- (void)setData:(NSArray *)data
{
    _cityArray = data;
    [_tableView reloadData];
}

- (void)showTable
{
    [_tableView setBackgroundColor:[UIColor whiteColor]];
    [_cityTitleLabel setTextColor:[UIColor blackColor]];
    [_pointImage setImage:[UIImage imageNamed:@"cityChooseSelected"]];
    [_arrowImage setImage:[UIImage imageNamed:@"cityArrowSelected"]];
    [_cityBar setBackgroundColor:[UIColor whiteColor]];
}

- (void)hideTable
{
    [_tableView setBackgroundColor:[UIColor clearColor]];
    [_cityTitleLabel setTextColor:[UIColor whiteColor]];
    [_pointImage setImage:[UIImage imageNamed:@"cityChoose"]];
    [_arrowImage setImage:[UIImage imageNamed:@"cityArrow"]];
    [_cityBar setBackgroundColor:[UIColor clearColor]];
    
}

- (void)setViewFrame:(BOOL)isShow
{
    _isShow = isShow;
    if (_isShow) {
        [self showTable];
    } else {
        [_tableView setContentOffset:CGPointZero];
    }
    
    if (_delegate && [_delegate respondsToSelector:@selector(didPressCityBar:)]) {
        [_delegate didPressCityBar:_isShow];
    }
    
    //获取动作位置
    CGRect rect = self.frame;
    rect.size.height = (isShow ? _trueRect.size.height + rect.origin.y: 30);
    
    //背景动画
    [UIView animateWithDuration:0.5f animations:^{
        [self setFrame:rect];
    } completion:^(BOOL finished) {
        if (!isShow) {
            [self hideTable];
        }
    }];
    
    //表格动画
    [UIView animateWithDuration:0.5f animations:^{
        [_tableView setFrame:CGRectMake(0, 0, rect.size.width, rect.size.height)];
    }];
}

- (void)simulationPressCityWithRow:(NSUInteger)row
{
    [self pressCityWithRow:row];
}

- (void)pressCityWithRow:(NSUInteger)row
{
    _currentRow = row;
    if (_delegate) {
        [_delegate didSelectedRow:row text:_cityArray[row]];
    }
    NSString * text = [[_cityArray[row] allValues] lastObject];
    [[self getCityTitleLabel] setText:text];
    [self setViewFrame:NO];
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self pressCityWithRow:indexPath.row];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40.0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _cityArray.count;
}

//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
//{
//    return [self getCityBar];
//}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return (DeviceType < 6 ? 30 : 40);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellIdent = @"cityCell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdent];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdent];
        cell.accessoryType = UITableViewCellAccessoryNone;              //无样式
        [cell.selectedBackgroundView setBackgroundColor:[UIColor clearColor]];
        [cell.textLabel setFont:(DeviceType < 6 ? [UIFont systemFontOfSize:12] : [UIFont systemFontOfSize:14])];
        [cell.textLabel setTextAlignment:NSTextAlignmentCenter];
    }
    NSString * cityName = [[_cityArray[indexPath.row] allValues] lastObject];
    [cell.textLabel setText: cityName];
    return cell;
}

@end
